﻿/* 
 * Copyright 2012 Pieter Roggemans
 *
 * This file is part of OWNMonitor.
 *
 * OWNMonitor is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, version 3 of the License.
 *
 * OWNMonitor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with OWNMonitor. 
 * If not, see http://www.gnu.org/licenses/.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;        // Added to use BindingList
using System.Xml.Serialization;     // Added to use serialisation
using System.IO;                    // Added to use serialisation to file

namespace OWNLib
{
    [Serializable()]	
    public class OWNRefData
    {
        [Serializable()]
        public class Direction
        {
            public string Id { get; set; }
            public string Code { get; set; }

            public Direction(string id, string code)
            {
                Id = id;
                Code = code;
            }

            public Direction()
            {
            }
        }

        [Serializable()]
        public class Who
        {
            public string Id { get; set; }
            public string Code { get; set; }

            public Who(string id, string code)
            {
                Id = id;
                Code = code;
            }

            public Who()
            {
            }

            public override string ToString()
            {
                return this.Code;
            }
        }

        [Serializable()]
        public class What
        {
            public string WhoId { get; set; }
            public string WhatId { get; set; }
            public string Code { get; set; }

            public What(string whoId, string whatId, string code)
            {
                WhoId = whoId;
                WhatId = whatId;
                Code = code;
            }

            public What()
            {
            }
        }

        [Serializable()]
        public class Dimension
        {
            public string WhoId { get; set; }
            public string DimId { get; set; }
            public string Code { get; set; }
            public string FormatMask { get; set; }

            public Dimension(string whoId, string dimId, string code, string formatMask)
            {
                WhoId = whoId;
                DimId = dimId;
                Code = code;
                FormatMask = formatMask;
            }

            public Dimension()
            {
            }
        }

        [Serializable()]
        public class TranslatedText
        {
            public string Id1 { get; set; }
            public string Id2 { get; set; }
            public string Id3 { get; set; }
            public int Language { get; set; }
            public string Text { get; set; }
        }

        public string RefDirectory { get; set; }
                
        public BindingList<Direction> Directions { get; set; }

        public BindingList<Who> Whos { get; set; }

        public BindingList<What> Whats { get; set; }

        public BindingList<What> WhatsFiltered { get; private set; }
        private string _whatFilter = "";

        public BindingList<Dimension> Dimensions { get; set; }


        public OWNRefData()
        {
            RefDirectory = "C:\\temp\\OWNRefData";
            Directions = new BindingList<Direction>();
            Whos = new BindingList<Who>();
            Whats = new BindingList<What>();
            WhatsFiltered = new BindingList<What>();
            Dimensions = new BindingList<Dimension>();
            
            //Directions = new BindingList<Direction>();
            //Directions.Add(new Direction("0", "Tx"));
            //Directions.Add(new Direction("1", "Rx"));
            //Directions.Add(new Direction("3", "Mon"));

            //Whos.Add(new Who("0", "Scenarios"));
            //Whos.Add(new Who("1", "Lighting"));
            //Whos.Add(new Who("2", "Automation"));
            //Whos.Add(new Who("3", "ControlloCarichi"));
            //Whos.Add(new Who("4", "TemperatureControl"));
            //Whos.Add(new Who("5", "Alarm"));
            //Whos.Add(new Who("6", "VDES"));
            //Whos.Add(new Who("13", "GatewayManagement"));
            //Whos.Add(new Who("15", "CENCommands"));
            //Whos.Add(new Who("16", "SoundDiffusion"));
            //Whos.Add(new Who("17", "MH200Scenarios"));
            //Whos.Add(new Who("18", "EnergyManagement"));
            //Whos.Add(new Who("25", "CENPlus"));
            //Whos.Add(new Who("2001", "AutomationDiagnostic"));
            //Whos.Add(new Who("1004", "ThermoregulationDiagnostic"));
            //Whos.Add(new Who("1013", "DeviceDiagnostic"));

            //Whats.Add(new What("1", "0", "OFF"));
            //Whats.Add(new What("1", "0#x", "OFF at x speed"));
            //Whats.Add(new What("1", "1", "ON"));
            //Whats.Add(new What("1", "1#x", "ON at x speed"));
            //Whats.Add(new What("1", "2", "20%"));
            //Whats.Add(new What("1", "3", "30%"));
            //Whats.Add(new What("1", "4", "40%"));
            //Whats.Add(new What("1", "5", "50%"));
            //Whats.Add(new What("1", "6", "60%"));
            //Whats.Add(new What("1", "7", "70%"));
            //Whats.Add(new What("1", "8", "80%"));
            //Whats.Add(new What("1", "9", "90%"));
            //Whats.Add(new What("1", "10", "100%"));
            //Whats.Add(new What("1", "11", "ON timed 1 min"));
            //Whats.Add(new What("1", "12", "ON timed 2 min"));
            //Whats.Add(new What("1", "13", "ON timed 3 min"));
            //Whats.Add(new What("1", "14", "ON timed 4 min"));
            //Whats.Add(new What("1", "15", "ON timed 5 min"));
            //Whats.Add(new What("1", "16", "ON timed 15 sec"));
            //Whats.Add(new What("1", "17", "ON timed 30 sec"));
            //Whats.Add(new What("1", "18", "ON timed 0,5 sec"));
            //Whats.Add(new What("1", "19", "NO Load only for dimmer)"));
            //Whats.Add(new What("1", "20", "Blinking for 0,5 sec"));
            //Whats.Add(new What("1", "21", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "22", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "23", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "24", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "25", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "26", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "27", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "28", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "29", "Blinking for 1 sec"));
            //Whats.Add(new What("1", "30", "Up one level (only for dimmer)"));
            //Whats.Add(new What("1", "30#level#x", "Up x levels at x speed (only for dimmer)"));
            //Whats.Add(new What("1", "31", "Down one level (only for dimmer)"));
            //Whats.Add(new What("1", "31#level#x", "Down x levels at x speed (only for dimmer)"));

            //// CEN-configured push buttons
            //Whats.Add(new What("15", "01", "Button top-left"));
            //Whats.Add(new What("15", "02", "Button top-right"));
            //Whats.Add(new What("15", "03", "Button bottom-left"));
            //Whats.Add(new What("15", "04", "Button bottom-right"));

            //Dimensions.Add(new Dimension("1", "1", "Set y level at x speed", ""));
            //Dimensions.Add(new Dimension("1", "2", "Timers", ""));

            //Dimensions.Add(new Dimension("4", "0", "Temperature", "{0:CELSIUS}"));
            //Dimensions.Add(new Dimension("4", "12", "CompleteProbeStatus", ""));
            //Dimensions.Add(new Dimension("4", "13", "LocalSetOffset", ""));
            //Dimensions.Add(new Dimension("4", "14", "SetPointTemperature", ""));
            //Dimensions.Add(new Dimension("4", "19", "ValvesStatus", ""));
            //Dimensions.Add(new Dimension("4", "30", "EndDateHolidayScenario", ""));
            //Dimensions.Add(new Dimension("4", "31", "EndHourHolidayScenario", ""));

            //Dimensions.Add(new Dimension("13", "0", "Time", "{0}:{1}:{2} {3:TIMEZONE}"));
            //Dimensions.Add(new Dimension("13", "1", "Date", "{0} {3}-{2}-{1}"));
            //Dimensions.Add(new Dimension("13", "10", "IPAddress", "{0}.{1}.{2}.{3}"));
            //Dimensions.Add(new Dimension("13", "11", "NetMask", "{0}.{1}.{2}.{3}"));
            //Dimensions.Add(new Dimension("13", "12", "MACAddress", "{0}:{1}:{2}:{3}:{4}:{5}:{6}"));
            //Dimensions.Add(new Dimension("13", "15", "DeviceType", "{0:MODEL}"));
            //Dimensions.Add(new Dimension("13", "16", "FirmwareVersion", "{0}.{1} build {2}"));
            //Dimensions.Add(new Dimension("13", "19", "UpTime", "{0} days, {1} hours, {2} min,  {3} sec"));
            //Dimensions.Add(new Dimension("13", "22", "DateAndTime", "{4:DAYOFWEEK} {7}-{6}-{5} {0}:{1}:{2} {3:TIMEZONE}"));
            //Dimensions.Add(new Dimension("13", "23", "KernelVersion", "{0}.{1} build {2}"));
            //Dimensions.Add(new Dimension("13", "24", "DistributionVersion", "{0}.{1} build {2}"));
        }

        public void SetFilterWhat(string whoId)
        {
            if (_whatFilter != whoId)
            {
                WhatsFiltered.Clear();
                foreach (What what in Whats)
                {
                    if (what.WhoId == whoId)
                        WhatsFiltered.Add(what);
                }
            }
        }

        public Direction FindDirection(string id)
        {
            foreach (Direction dir in Directions)
            {
                if (dir.Id == id)
                    return dir;
            }
            return new Direction(id, "Unknown");
        }

        public Who FindWho(string id)
        {
            foreach (Who who in Whos)
            {
                if (who.Id == id)
                    return who;
            }
            return new Who(id, "Unknown");
        }

        public What FindWhat(string whoId, string whatId)
        {
            foreach (What what in Whats)
            {
                if (what.WhoId == whoId && what.WhatId == whatId)
                    return what;
            }
            return new What(whoId, whatId, "Unknown");
        }

        public Dimension FindDimension(string whoId, string dimId)
        {
            foreach (Dimension dim in Dimensions)
            {
                if (dim.WhoId == whoId && dim.DimId == dimId)
                    return dim;
            }
            return new Dimension(whoId, dimId, "Unknown", "");
        }

        public void Serialize()
        {
            Directory.CreateDirectory(RefDirectory);

            // Serialize directions to file
            XmlSerializer serializer = new XmlSerializer(typeof(BindingList<Direction>));
            using (TextWriter tw = new StreamWriter(RefDirectory + "\\Directions.xml"))
                serializer.Serialize(tw, this.Directions);

            // Serialize whos to file
            serializer = new XmlSerializer(typeof(BindingList<Who>));
            using (TextWriter tw = new StreamWriter(RefDirectory + "\\Whos.xml"))
                serializer.Serialize(tw, this.Whos);

            // Serialize whats to file
            serializer = new XmlSerializer(typeof(BindingList<What>));
            using (TextWriter tw = new StreamWriter(RefDirectory + "\\Whats.xml"))
                serializer.Serialize(tw, this.Whats);

            // Serialize dimensions to file
            serializer = new XmlSerializer(typeof(BindingList<Dimension>));
            using (TextWriter tw = new StreamWriter(RefDirectory + "\\Dimensions.xml"))
                serializer.Serialize(tw, this.Dimensions);
        }

        public void DeSerialize()
        {
            // Deserialize direction
            XmlSerializer serializer = new XmlSerializer(typeof(BindingList<Direction>));
            using (TextReader tr = new StreamReader(RefDirectory + "\\Directions.xml"))
                Directions = (BindingList<Direction>)serializer.Deserialize(tr);

            // Deserialize whos
            serializer = new XmlSerializer(typeof(BindingList<Who>));
            using (TextReader tr = new StreamReader(RefDirectory + "\\Whos.xml"))
                Whos = (BindingList<Who>)serializer.Deserialize(tr);

            // Deserialize whats
            serializer = new XmlSerializer(typeof(BindingList<What>));
            using (TextReader tr = new StreamReader(RefDirectory + "\\Whats.xml"))
                Whats = (BindingList<What>)serializer.Deserialize(tr);

            // Deserialize dimensions
            serializer = new XmlSerializer(typeof(BindingList<Dimension>));
            using (TextReader tr = new StreamReader(RefDirectory + "\\Dimensions.xml"))
                Dimensions = (BindingList<Dimension>)serializer.Deserialize(tr);
        }
    }
}
